package com.yami.shop.rights.common.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.bean.event.ProdRightsEvent;
import com.yami.shop.bean.vo.ProdRightsVo;
import com.yami.shop.rights.common.model.ProdRights;
import com.yami.shop.rights.common.service.PersonRightsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component("ProdRightsListener")
@AllArgsConstructor
public class ProdRightsListener {

    @Autowired
    private PersonRightsService personRightsService;

    @EventListener(ProdRightsEvent.class)
    public void getProdRights(ProdRightsEvent event) {
        try {
            // 获取权益信息
            if(ObjectUtil.isEmpty(event.getRightsId()) || ObjectUtil.isEmpty(event.getUserId())){
                return;
            }
            List<ProdRights> prodRights = personRightsService.listUserByRightsIds(event.getUserId(),event.getRightsId());
            event.setProdRights(BeanUtil.copyToList(prodRights, ProdRightsVo.class));
        } catch (Exception e) {
            log.error("获取该商品权益信息失败:", e);
        }
    }

}
